#import "SEGPickView.h"

@implementation SEGPickViewModel
-(instancetype)init{
    if (self == [super init]) {
        _content = @"";
        _widgetWidth = [UIScreen mainScreen].bounds.size.width;
    }
    return self;
}
@end

/**************************SEGPickViewLayout************************/
@implementation SEGPickViewLayout
- (instancetype)initWithData:(SEGPickViewModel *)data type:(SEGEnumPickViewType)type{
    if (self == [super init]) {
        _data = data;
        _type = type;
    }
    [self segPickViewLayout];
    return self;
}

-(void)segPickViewLayout{
    //高度固定
    _totalHeight = kPickViewHeight;
    
    //高度不固定
    //_totalHeight = 0;
    _contentLabelHeight = 0;
    _contentLabelTextLayout = nil;
    
    if (!_data) return;
    
    [self layout];
}

//数据处理
-(void)layout{
    [self segcontentLabelWithContent:_data.content fontSize:16 textColor:kColor_1];
}
//控件contentLabel文本布局计算
-(void)segcontentLabelWithContent:(NSString*)content fontSize:(CGFloat)fontSize textColor:(UIColor*)color{
    if(!content)return;
    UIFont *font = [UIFont fontWithName:@"Heiti SC" size:fontSize];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:content];
    text.yy_font = font;
    text.yy_color = color;
    
    //SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    //modifier.font = font;
    //modifier.paddingTop = 1;
    //modifier.paddingBottom = 1;
    //modifier.lineHeightMultiple = 1;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(_data.widgetWidth, HUGE);
    //container.linePositionModifier = modifier;
    
    _contentLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    //if (!_contentLabelTextLayout)return;
    //_contentLabelHeight = [modifier heightForLineCount:_contentLabelTextLayout.rowCount];
}

@end

/**************************SEGPickView************************/
@implementation SEGPickView
- (instancetype)initWithFrame:(CGRect)frame {
    if (frame.size.width == 0 && frame.size.height == 0) {
        frame.size.width = [UIScreen mainScreen].bounds.size.width;
        frame.size.height = kPickViewHeight;
    }
    self = [super initWithFrame:frame];
    
    [self addSubview:self.contentLabel];
    
    if (kDebug)self.backgroundColor = kSegRandomColor;
    return self;
}

-(void)setWithLayout:(SEGPickViewLayout *)layout{
    self.height = layout.totalHeight;
    self.width = layout.data.widgetWidth;
    _layout = layout;
    
    if (self.height <= 0) return;
    
    self.contentLabel.textLayout = layout.contentLabelTextLayout;
    
    [self setupUI];
}

//界面布局
-(void)setupUI{
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
    }];
    
    self.contentLabel.textAlignment = NSTextAlignmentCenter;
    self.contentLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
}

#pragma mark - getter
-(SEGLabel *)contentLabel{
    if (!_contentLabel) {
        _contentLabel = [[SEGLabel alloc]init];
        _contentLabel.userInteractionEnabled = NO;
        _contentLabel.textAlignment = NSTextAlignmentCenter;
        _contentLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
        _contentLabel.displaysAsynchronously = YES;
        _contentLabel.ignoreCommonProperties = YES;//YES会影响系统属性
        _contentLabel.fadeOnHighlight = NO;
        _contentLabel.numberOfLines = 0;
        _contentLabel.fadeOnAsynchronouslyDisplay = NO;
        _contentLabel.fadeOnAsynchronouslyDisplay = NO;
        _contentLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        if (kDebug)_contentLabel.backgroundColor = kSegRandomColor;
        //__weak typeof(self) _self = self;
        //_contentLabel.textTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        //[SEGAlertView showTmepMessageWithMessage:@"_contentLabelClick"];
        //if (_self.delegate && [_self.delegate respondsToSelector:@selector(SEGprotocolcontentLabel:)]) {
        //[_self.delegate SEGprotocolcontentLabel:_self.contentLabel];
        //}
        //};
    }
    return _contentLabel;
}


@end

